package edoc.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

import edoc.entity.EdocCategory;
import edoc.entity.EdocEntry;
import edoc.service.CategoryService;
import edoc.service.EntryService;

public class EntryAction extends ActionSupport {

	private EntryService entryService;
	/**
	 * 
	 */
	private List<EdocEntry> entryList;

	/**
	 * setter注入
	 * 
	 * @param entryService
	 */
	public void setEntryService(EntryService entryService) {
		this.entryService = entryService;
	}

	public List<EdocEntry> getEntryList() {
		return entryList;
	}

	public void setEntryList(List<EdocEntry> entryList) {
		this.entryList = entryList;
	}

	private Long categoryId;

	public Long getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Long categoryId) {
		this.categoryId = categoryId;
	}

	/**
	 * 调用service层的findAll()方法
	 * 
	 * @return 成功返回success,失败返回input
	 */
	@Override
	public String execute() throws Exception {
		try {
			entryList = entryService.findAll(categoryId);
			categoryList = entryService.findCategory();
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	// ----------------删除操作-------------------
	private Long id;
	private String message;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * 删除方法
	 */
	public String delEntry() throws Exception {
		message = null;
		try {
			entryService.del(id);
			this.message = "删除成功";
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			this.message = "删除失败";
			return INPUT;
		}
	}

	// ----------------添加------------------
	/***
	 * 添加电子文档信息
	 * 
	 */
	private EdocEntry entry;

	public EdocEntry getEntry() {
		return entry;
	}

	public void setEntry(EdocEntry entry) {
		this.entry = entry;
	}

	public String showAdd() throws Exception {

		return SUCCESS;
	}

	public String addEntry() throws Exception {
		try {
			EdocCategory edocCategory = new EdocCategory();
			edocCategory.setId(categoryId);
			entry.setEdocCategory(edocCategory);
			entryService.addEntry(entry);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	// -------------通过id查到某条记录--------------
	public String findById() throws Exception {
		try {
			entry = entryService.findById(id);
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return INPUT;
		}
	}

	// ------------修改-------------
	public String updateEntry() throws Exception {
		try {
			EdocCategory edocCategory = new EdocCategory();
			edocCategory.setId(categoryId);
			entry.setEdocCategory(edocCategory);
			entryService.update(entry);
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return INPUT;
		}
	}

	// -------查询电子文档类别信息-------------
	private CategoryService categoryService;
	private List<EdocCategory> categoryList;

	public List<EdocCategory> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<EdocCategory> categoryList) {
		this.categoryList = categoryList;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}
	public String shwAdd() throws Exception {
		
		return SUCCESS;
	}
}
